package com.youtellv1.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youtellv1.entity.ChildPay;
import com.youtellv1.mapper.ChildPayMapper;
import com.youtellv1.service.ChildPayService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChildPayServiceImpl implements ChildPayService {
    @Resource
    private ChildPayMapper childPayMapper;


    @Override
    public PageInfo<ChildPay> findChildPayByPage(int page, int size) {
        int offset = (page - 1) * size;
        List<ChildPay> list = childPayMapper.findAllChildPay(offset, size);

        int total = childPayMapper.childPayCount();

        int pages = (total + size - 1) / size;

        PageInfo<ChildPay> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(size);
        pageInfo.setPages(pages);  // 关键：设置总页数

        return pageInfo;
    }

    @Override
    public PageInfo<ChildPay> findChildPayByCid(int page, int size, int cid) {
        int offset = (page - 1) * size;
        List<ChildPay> list = childPayMapper.findChildPayByCidLimit(cid, offset, size);

        int total = childPayMapper.countByCid(cid);

        int pages = (total + size - 1) / size;

        PageInfo<ChildPay> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(size);
        pageInfo.setPages(pages);  // 关键：设置总页数

        return pageInfo;
    }

    @Override
    public ChildPay findById(Integer cpid) {
        return childPayMapper.findById(cpid);
    }

    @Override
    public Integer childPayCountService() {
        return childPayMapper.childPayCount();
    }

    @Override
    public Map<String, Object> getPayList(Integer cid, String month, int page, int size) {
        String startDate = null;
        String endDate = null;

        if (month != null && !month.isEmpty()) {
            LocalDate start = LocalDate.parse(month + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            LocalDate end = start.withDayOfMonth(start.lengthOfMonth());
            startDate = start.toString();
            endDate = end.toString();
        }

        int offset = (page - 1) * size;
        List<ChildPay> list = childPayMapper.findPage(cid, startDate, endDate, offset, size);
        int total = childPayMapper.count(cid, startDate, endDate);
        int pages = (int) Math.ceil((double) total / size);

        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        result.put("pages", pages);

        return result;
    }



    @Override
    public ChildPay findAllService(ChildPay childPay) {
        return childPayMapper.findChildPayById(childPay);
    }

    @Override
    public void saveService(ChildPay childPay) {
        childPayMapper.save(childPay);
    }

    @Override
    public void updateService(ChildPay childPay) {
        childPayMapper.update(childPay);
    }

    @Override
    public void deleteService(ChildPay childPay) {
        childPayMapper.delete(childPay);
    }

    @Override
    public Integer countChildService() {
        return childPayMapper.countChild();
    }

    @Override
    public Float countChildPayService() {
        return childPayMapper.countChildPay();
    }
}
